/*
 * @Author: gonglf
 * @Date: 2022-08-15 09:59:17
 * @LastEditors: gonglf
 * @LastEditTime: 2022-08-19 20:09:26
 * @Description:
 *
 */
package main

import (
	"fmt"
	"log"
	"net/rpc/jsonrpc"
	"time"
)

/*
1、serviceMethod大小写敏感，写错了会有err，没有panic，连接不会断开
2、args类型错了，不会断开连接，服务端可能执行也可能不执行，结果可能错误也可能不错误，就看离谱程度
2、reply类型搞错了，连接就会断开
2、客户端返回类型搞错了，也不会报错
3、客户端可以通过一个client多次请求
*/

type Args struct {
	A, B int
}

const (
	URL = "127.0.0.1:5002"
)

func main() {

	client, err := jsonrpc.Dial("tcp", URL)
	defer client.Close()

	if err != nil {
		fmt.Println(err)
	}

	var args Args = Args{1, 2}

	harvester_time1 := time.Now().UnixNano()
	var reply int

	go func() {
		var reply2 string
		err = client.Call("HelloService.Hello", "glf", &reply2)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(reply2)
	}()

	err = client.Call("Arith.Add", args, &reply)
	if err != nil {
		fmt.Println(err)
	}
	harvester_time2 := time.Now().UnixNano()

	fmt.Printf("cal time %f \n", float64(harvester_time2-harvester_time1)/float64(1000000000))
	fmt.Println(reply)

	time.Sleep(7 * time.Second)

}
